﻿﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using CrossesGrav.GameLogic;
using CrossesGrav.WorkClasses;
using System.Windows.Forms;


namespace CrossesGrav
{
    public partial class Form1 : Form
    {

       

        private int fieldsize = 0;
        //Panel m_items = new Panel();
        int defx;
        int defy;
        int begx;
        int begy;
        //int w =40;
        //int h = 40;

        int client_w;
        int client_h;
        int window_w;
        int window_h;
        public int w;
        public int h;
       // int movements;

        Panel layout;

        List<Cell> Cells = new List<Cell>();
        List<int> CountInStrings = new List<int>();
        List<List<int>> StringNumbers = new List<List<int>>();
        List<Player> pls = new List<Player>();

        // static public Form TestForm;//= new Form();


         Player Gamer1;
         Player Gamer2;


        int Field_Size
        {
            get
            {
                return fieldsize;
            }
            set
            {
                fieldsize = value;
            }
        }
        int Default_X
        {
            get
            {
                return defx;
            }
            set
            {
                defx = value;
            }
        }
        int Default_Y
        {
            get
            {
                return defy;
            }
            set
            {
                defy = value;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        public void MakeLayout(int n)
        {// Рисуй компоненты на панель в памяти, а не сразу на форму. Не трать ресурсы.
            switch (n)
            {
                case 0:
                    {
                        MakeMenuBack(30, 90, 170, 110);

                        label2.Parent = layout;
                        label2.Left = 0;
                        label2.Top = 8;

                        textBox2.Parent = layout;
                        textBox2.Left = label2.Bounds.Right;
                        textBox2.Top = 5;

                        label3.Parent = layout;
                        label3.Left = 0;
                        label3.Top = label2.Top + label2.Height+10;

                        /*
                       radioButton1.Parent = layout;
                       radioButton1.Top = label3.Top;
                       radioButton1.Left = label3.Bounds.Right;

                       radioButton2.Parent = layout;
                       radioButton2.Top = label3.Top;
                       radioButton2.Left = radioButton1.Bounds.Right + 10;
                       */

                        panel3.Parent = layout;
                        panel3.Top = label3.Top-5;
                        panel3.Left = label3.Bounds.Right;

                        label1.Parent = layout;
                        label1.Left = 0;
                        label1.Top = label3.Top + label3.Height + 15;

                        textBox1.Parent = layout;
                        textBox1.Top = label1.Top-5;
                        textBox1.Left = label1.Bounds.Right;

                        button1.Parent = layout;
                        button1.Top = textBox1.Top + textBox1.Height + 10;
                        button1.Left = 2;

                        button2.Parent = layout;
                        button2.Top = button1.Top;
                        button2.Left = button1.Bounds.Right;

                        layout.Visible = true;

                        break;
                    }

                case 1:
                    {
                        MakeMenuBack(30, 90, 360, 130);

                        label2.Parent = layout;
                        label2.Left = 0;
                        label2.Top = 8;

                        textBox2.Parent = layout;
                        textBox2.Left = label2.Bounds.Right;
                        textBox2.Top = 5;

                        label3.Parent = layout;
                        label3.Left = 0;
                        label3.Top = label2.Top + label2.Height + 10;

                        /*
                        radioButton1.Parent = layout;
                        radioButton1.Top = label3.Top;
                        radioButton1.Left = label3.Bounds.Right;

                        radioButton2.Parent = layout;
                        radioButton2.Top = label3.Top;
                        radioButton2.Left = radioButton1.Bounds.Right + 10;
                        */

                        panel3.Parent = layout;
                        panel3.Top = label3.Top-5;
                        panel3.Left = label3.Bounds.Right;

                        
                        //
                        label4.Parent = layout;
                        label4.Left = label2.Right+textBox2.Right;
                        label4.Top = label2.Top;

                        textBox3.Parent = layout;
                        textBox3.Left = label4.Bounds.Right;
                        textBox3.Top = 5;

                        label5.Parent = layout;
                        label5.Left = label4.Left;
                        label5.Top = label2.Top + label2.Height + 10;

                        /*
                        radioButton3.Parent = layout;
                        radioButton3.Top = label3.Top;
                        radioButton3.Left = label5.Bounds.Right;

                        radioButton4.Parent = layout;
                        radioButton4.Top = label3.Top;
                        radioButton4.Left = radioButton3.Bounds.Right + 10;
                        */

                        panel4.Parent = layout;
                        panel4.Top = label3.Top-5;
                        panel4.Left = label5.Bounds.Right;

                        label6.Parent = layout;
                        label6.Left = 0;
                        label6.Top = label3.Top + label3.Height+10;

                        panel5.Parent = layout;
                        panel5.Top = label6.Top-5;
                        panel5.Left = label6.Right;

                        label7.Parent = layout;
                        label7.Left = label5.Left;
                        label7.Top = label5.Top + label5.Height + 10;

                        panel6.Parent = layout;
                        panel6.Top = label7.Top - 5;
                        panel6.Left = label7.Right;
                        
                        //
                        label1.Parent = layout;
                        label1.Left = 0;
                        label1.Top = panel5.Top + panel5.Height + 25;

                        textBox1.Parent = layout;
                        textBox1.Top = label1.Top-3;
                        textBox1.Left = label1.Bounds.Right;

                        button1.Parent = layout;
                        button1.Top = textBox1.Top-3;// +textBox1.Height + 10;
                        button1.Left = textBox1.Right+5;

                        button2.Parent = layout;
                        button2.Top = button1.Top;
                        button2.Left = button1.Bounds.Right;

                        layout.Visible = true;

                        break;
                    }

                default:
                    {
                        break;
                    }
            }
        }

        private void MakeMenuBack(int x, int y, int width, int heigth)
        {
            layout = new Panel();
            Panel t = layout;
            t.Visible = false;
            t.Top = y;
            t.Left = x;
            t.Width = width;
            t.Height = heigth;
            t.BackColor = Color.FromArgb(180, 243, 230, 172);
            t.Parent = panel1;
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            pictureBox1 = Game.fonts["[Гравики]", pictureBox1, false];
           pictureBox2 = Game.fonts["Один игрок", pictureBox2, true];
           pictureBox3 = Game.fonts["Два игрока", pictureBox3, true];
           pictureBox4 = Game.fonts["Справка", pictureBox4,false];
           pictureBox5 = Game.fonts["Выход", pictureBox5,false];
           pictureBox6 = Game.fonts[" Рестарт", pictureBox6,false];
           pictureBox7 = Game.fonts["Возврат", pictureBox7,false];

           Game.menu_items.Add(pictureBox2);
           Game.menu_items.Add(pictureBox3);
           Game.menu_items.Add(pictureBox4);
           Game.menu_items.Add(pictureBox5);

           Game.images.Add("Cross", global::CrossesGrav.Properties.Resources.Cross);
           Game.images.Add("Circle", global::CrossesGrav.Properties.Resources.Circle);
           Game.images.Add("Free", global::CrossesGrav.Properties.Resources.Free);
           Game.images.Add("Cross_Win", global::CrossesGrav.Properties.Resources.Cross_Win);

        }

        private void Creator(int c)
        {
            int temp = 1;
            int num = 0;
            int tempX = begx;
            Stack<int> length = new Stack<int>();

            pls.Add(Gamer1);
            pls.Add(Gamer2);

            for (int i = 0; i < c * 2 - 1; i++)
            {

                {
                    StringNumbers.Add(new List<int>());

                    if (i < c - 1)
                    {
                        temp = i * 2 + 1;
                        length.Push(temp);
                        tempX -= w;
                    }
                    else
                        if (i == c - 1)
                        {
                            temp = i * 2 + 1;
                            tempX -= w;
                        }
                        else
                        {
                            temp = length.Pop();
                            tempX += w;
                        }
                    for (int j = 0; j < temp; j++)
                    {
                        //******************
                        //List<Player> pls = new List<Player>();

                        //******************


                        /* УДАЛИТЬ


                        TextBox t1 = new TextBox();
                        // TextBox t2 = new TextBox();
                        TextBox t3 = new TextBox();

                        t1.Top = begy + (h) * i;
                        t1.Left = tempX + (w) * j;
                        t1.Parent = this;
                        t1.Width = 38;
                        // t1.Hide();
                        
                        t2.Top = begy + (h) * i;
                        t2.Left = tempX + (w) * j+t1.Width;
                        t2.Parent = this;
                        t2.Width = 20;
                        
                        t3.Top = begy + (h) * i + t1.Height;
                        t3.Left = tempX + (w) * j;
                        t3.Parent = this;
                        t3.Width = 17;
                        t3.Text = Convert.ToString(num);
                        //t1.Text = Convert.ToString(num);
                        Game.tmpInd.Add(t1);
                        // Game.tmpInd1.Add(t2);
                        Game.tmpInd2.Add(t3);
                        //УДАЛИТЬ*/


                        Cell t = new Cell(h, w, pls);
                        t.stat.Free = true;
                        t.stat.Acessibility = false;
                        t.Box.Parent = this;
                        t.Y_Coord = begy + (h) * i;
                        t.X_Coord = tempX + (w) * j;
                        t.Number = num++;


                        StringNumbers[i].Add(t.Number);
                        Cells.Add(t);

                    }
                    CountInStrings.Add(temp);

                }
            }
        }

        private void Connector(int c)
        {
            Dictionary<int, int> begins = new Dictionary<int, int>();
            int beg = 0;
            Sides tmp = new Sides();
            for (int i = 0; i < c * 2 - 1; i++)
            {
                begins.Add(i, beg);
                beg += CountInStrings[i];
            }

            for (int i = 0; i < c * 2 - 1; i++)
            {
                int fst = begins[i];

                for (int j = 0; j < CountInStrings[i]; j++)
                {

                    if ((j + 1) < CountInStrings[i])
                    {
                        tmp.obj = Cells[fst + j + 1]; // Запад -> восток и наоборот
                        tmp.Connected = true;
                        Cells[fst + j].East = tmp;
                    }
                    if (i < c - 1)
                    {

                        tmp.obj = Cells[fst + j + CountInStrings[i] + 1]; // Север -> юг
                        tmp.Connected = true;
                        Cells[fst + j].South = tmp;
                    }
                    if (i > c - 1)
                    {
                        tmp.obj = Cells[(fst + j) - (CountInStrings[i] + 1)]; // Юг -> север
                        tmp.Connected = true;
                        Cells[fst + j].Nord = tmp;
                    }

                }
            }
            Preparer(c, begins);
        }

        private void Preparer(int c, Dictionary<int, int> b)
        {
            Cell t = new Cell();
            Cell t1 = new Cell();
            int index = b[c - 1];
            for (int i = 0; i < CountInStrings[c - 1]; i++)
            {
                t = Cells[index];

                while (t != null)
                {
                    t1 = t;
                    t = t.South.obj;
                }
                index++;
                t1.stat.Acessibility = true;
                Game.AvalibleCells.Add(t1);
            }

            for (int q = 0; q < Cells.Count; q++)
            {
                Cells[q].SideList.Add(Cells[q].N_East);
                Cells[q].SideList.Add(Cells[q].East.obj);
                Cells[q].SideList.Add(Cells[q].S_East);
                Cells[q].SideList.Add(Cells[q].South.obj);
                Cells[q].SideList.Add(Cells[q].S_West);
                Cells[q].SideList.Add(Cells[q].West.obj);
                Cells[q].SideList.Add(Cells[q].N_West);
                Cells[q].SideList.Add(Cells[q].Nord.obj);
            }

        }

        private void Gamers_config()
        {

            Gamer1.moving = true;
            Gamer2.moving = false;
            Gamer1.can_click = true;
            Gamer2.can_click = true;

            Gamer1.Cells = Cells;
            Gamer2.Cells = Cells;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox6.Visible = true;
            pictureBox7.Visible = true;
            pictureBox9.Visible = true;
            pictureBox8.Visible = true;

            if (radioButton5.Checked == true)
            {
                Gamer1 = new Human();
            }
            else Gamer1 = new AI();

            if (radioButton7.Checked == true)
            {
                Gamer2 = new AI();
            }
            else Gamer2 = new Human();

            if (radioButton1.Checked == true)
            {
                Gamer1.side = true;
                Gamer2.side = false;
            }
            else
            {
                Gamer1.side = false;
                Gamer2.side = true;
            }

            Gamer1.name = textBox2.Text;
            Gamer2.name = textBox3.Text;

            pictureBox9 = Game.fonts[Gamer1.name, pictureBox9, false];
            pictureBox8 = Game.fonts[Gamer2.name, pictureBox8, false];

            panel1.Hide();
            Field_Size = Convert.ToInt32(textBox1.Text) + 1;

            w = this.panel1.Size.Width / (fieldsize * 2 + 1);
            h = this.panel1.Size.Height / (fieldsize * 2 + 1);

            Default_X = this.panel1.Size.Height / 2 + h / 2;
            Default_Y = this.panel1.Size.Width / 2 - w / 2;
            begy = Default_Y - h * (Field_Size - 1)+12;
            begx = Default_X-12;
            
            Creator(Field_Size);
            Connector(Field_Size);
            Gamers_config();

            button2_Click(new object(), new EventArgs());

            Game.forWin = Field_Size - 1;

            Game.movements = Cells.Count();
            Game.lets_game(pls);

        }

        /* private void testModeEnabler()
         {
             TestForm = new Form();
             //TestForm.Parent = this;
             TestForm.Height = 500;
             TestForm.Width = 400;
             TestForm.Show();
             TestForm.Owner = this;
             TestForm.TextChanged += new EventHandler(TestForm_TextChanged);
             TextBox t = new TextBox();
             t.Multiline = true;
             t.Parent = TestForm;
             t.Dock = DockStyle.Fill;
             t.ScrollBars = ScrollBars.Both;

             string temp = "";
             for (int i = 0; i < textBox4.Text.Length; i++)
             {
                 switch (textBox4.Text[i])
                 {
                     case ' ':
                     case ',':
                         {
                             Array.Resize(ref Game.see_cells, Game.see_cells.Length + 1);
                             Game.see_cells[Game.see_cells.Length - 1] = Convert.ToInt32(temp);
                             temp = "";
                             break;
                         }
                    
                     default: temp += textBox4.Text[i]; break;
                 }
             }

         }
         */

        //   void TestForm_TextChanged(object sender, EventArgs e)
        // {
        // TestForm.Controls[0].Text = TestForm.Text;
        //}


        /* private void lets_game()
         {
             movements = Cells.Count();
             if ((Gamer1.moving == true) && (Gamer1.can_click == true))
                 Gamer1.Move(movements);
             else
                 if (Gamer2.can_click == true)
                     Gamer2.Move(movements);
             //Form1.ActiveForm.Refresh(); 
                    
         }
         */

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
           // textBox1.Text = Convert.ToString(trackBar1.Value);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            radioButton4.Checked = radioButton1.Checked;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            radioButton3.Checked = radioButton2.Checked;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            radioButton2.Checked = radioButton3.Checked;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            radioButton1.Checked = radioButton4.Checked;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
           // Game.test = checkBox1.Checked;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
           // pictureBox1.Image = Game.fonts.fonts[textBox4.Text[textBox4.Text.Length]];
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            //int temp;
           // int temp1;
            if ((client_h != ClientSize.Height) || (client_w != ClientSize.Width))
           // for (int i = 0; i < Cells.Count; i++)
            {
                //for (int i = 0; i < Cells.Count; i++)
                {
                 
                   // if (i!=0)
                   // Cells[i].Y_Coord -= (client_h - ClientSize.Height)*2;

                    for (int i = 0; i < StringNumbers.Count; i++)
                    {
                        //for (int j = 0; j < StringNumbers[i].Count; j++)
                        //{
                        //    if ((temp = ((client_h - ClientSize.Height) / CountInStrings.Count)) >= 1)
                        //        Cells[StringNumbers[i][j]].Box.Height -= temp;
                        //    else this.Height = window_h;
                        //    if ((temp1 = ((client_w - ClientSize.Width) / CountInStrings.Count)) >= 1)
                        //        Cells[StringNumbers[i][j]].Box.Width -= temp1;
                        //    else this.Width = window_w;


                        //    if (i != 0)
                        //        Cells[StringNumbers[i][j]].Y_Coord = Cells[StringNumbers[i - 1][0]].Box.Bounds.Top + Cells[StringNumbers[i - 1][0]].Box.Bounds.Height;
                        //    if (j != 0)  
                        //        Cells[StringNumbers[i][j]].X_Coord = Cells[StringNumbers[i][j - 1]].Box.Bounds.Left + Cells[StringNumbers[i - 1][0]].Box.Bounds.Width;
                        //    else
                        //        for (int k = 0; k < CountInStrings[i]; k++)
                        //            Cells[StringNumbers[i][j]].X_Coord -= Cells[StringNumbers[i][j]].Box.Bounds.Left;
                        //}
                    }
                }

                panel1.Height -= client_h - ClientSize.Height;
                panel1.Width -= client_w - ClientSize.Width;
            }

           //for (int i = 0; i<fie)
           // MessageBox.Show("qwe");
           // int temp = 1;
            
        }

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            client_h = ClientSize.Height;
            client_w = ClientSize.Width;

            window_h = this.Height;
            window_w = this.Width;
        }

        private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            //if (!Game.menu_click)
            //{
            //    for (int i = 0; i < pictureBox3.Width / 10; i++)
            //    {
            //        pictureBox3.Left -= pictureBox3.Width / 20;
            //        panel1.Refresh();
            //    }

            //}
            //else
            //{
            //    for (int i = 0; i < pictureBox3.Width / 10; i++)
            //    {
            //        pictureBox3.Left += pictureBox3.Width / 20;
            //        panel1.Refresh();

            //    }
            //}

            //Game.menu_click = !Game.menu_click;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Component []c = new Component[0];
            //  layout.Container.Components.CopyTo(c,0);

            radioButton7.Checked = true;
            radioButton5.Checked = true;
            radioButton1.Checked = true;
            textBox2.Text = "Игрок1";
            textBox3.Text = "Игрок2";
            textBox1.Text = "4";

            if (layout != null)
            {
                int temp = layout.Controls.Count;
                for (int i = 0; i < temp; i++)
                {
                    layout.Controls[0].Parent = panel2;
                }

                layout.Hide();
                layout = null;
                Game.can_be_clicked = true;
                Game.Click_Item(Game.now_clicked, new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 1));
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            Reset();
            pictureBox6.Visible = false;
            pictureBox7.Visible = false;
        }

        private void Reset()
        {
            for (int i = 0; i < Cells.Count; i++)
            {
                Cells[i].Box.Visible = false;
            }

            Cells = new List<Cell>();
            CountInStrings = new List<int>();
            StringNumbers = new List<List<int>>();
            pls = new List<Player>();


            Game.menu_click = false;
            Game.plr = null;
            Game.GameOver = false;
            Game.AvalibleCells = new List<Cell>();
            //Game.images = new List<Bitmap>();
            Game.now_clicked = null;
            Game.can_be_clicked = true;
            Game.menu_layout = -1;
        }

        public void pictureBox6_Click(object sender, EventArgs e)
        {
            Reset();

            Creator(Field_Size);
            Connector(Field_Size);
            Gamers_config();
            Game.movements = Cells.Count();
            Game.lets_game(pls);
           
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            textBox3.Text = "Компьютер";
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


    }
}